package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.ReFundBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * 退款
 */
public class RefundActivity extends BaseActivity {


    @BindView(R.id.iv_back_activity_refund)
    ImageView mIvBackActivityRefund;
    @BindView(R.id.tv_monety_activity_refund)
    TextView mTvMonetyActivityRefund;
    @BindView(R.id.tv_time_activity_refund)
    TextView mTvTimeActivityRefund;
    private LoadingPager mLoadingPager;
    private long mLongExtra;

    /**
     * 重新加载
     */
    private void retry() {

        Observable<StatusCode<ReFundBean>> refundDetailsObservable = ApiUtils.getApi().getRefundDetails(mLongExtra, BaseApplication.sUserInfoBean.getToken(),BaseApplication.sUserInfoBean.getUserId());
        HttpUtil.getInstance().toSubscribe(refundDetailsObservable, new ProgressSubscriber<ReFundBean>(this) {
            @Override
            protected void _onNext(StatusCode<ReFundBean> reFundBeanStatusCode) {
                ReFundBean data = reFundBeanStatusCode.getData();
                if (reFundBeanStatusCode.isFlag()&&data!=null){
                    mLoadingPager.showPagerView(Contans.STATE_SUCCEED);

                    mTvMonetyActivityRefund.setText(data.getRefund()+"元");
                    mTvTimeActivityRefund.setText(data.getRefundTime());

                }else {

                    mLoadingPager.showPagerView(Contans.STATE_EMPTY);
                }




            }

            @Override
            protected void _onError(String message) {
                mLoadingPager.showPagerView(Contans.STATE_ERROR);
                ToastUtils.makeText(message);


            }
        },"",lifecycleSubject,false,true);
    }

    @Override
    protected void initData() {
        super.initData();

        Intent intent = getIntent();
        mLongExtra = intent.getLongExtra(Contans.INTENT_DATA, 0);
retry();





    }

    @Override
    protected void initView() {

        mLoadingPager = new LoadingPager(this) {
            @Override
            protected void close() {
                finish();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(RefundActivity.this,R.layout.activity_refund);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };

      //  setContentView(R.layout.activity_refund);
        setContentView(mLoadingPager);
    }

    public static void startActivity(Context context, long orderId) {
        Intent intent = new Intent(context, RefundActivity.class);
        intent.putExtra(Contans.INTENT_DATA, orderId);
        context.startActivity(intent);
    }



    @OnClick(R.id.iv_back_activity_refund)
    public void onViewClicked() {
        finish();
    }
}
